//
//  GuidePlayPictureViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/8.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "GuidePlayPictureViewController.h"
#import "NetworkHandler.h"
#import "PlayPictureTableViewCell.h"
#import "PlayPictureModel.h"
#import "GuideHomeViewController.h"
#import "ToolModel.h"
#import "TAMacro.h"

@interface GuidePlayPictureViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)NSMutableArray * array;
@property (nonatomic,retain)UITableView * tableView;
@property (nonatomic,retain)PlayPictureTableViewCell * cell;
@end

@implementation GuidePlayPictureViewController

- (NSMutableArray *)array {
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    
    [self analysis];
    [self FoundTableView];
}
- (void)analysis {
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?limit=20&offset=0",self.str] BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"posts"];
        for (NSDictionary *dic in arr) {
            PlayPictureModel *model = [PlayPictureModel modelWithDic:dic];
            [self.array addObject:model];
            [self.tableView reloadData];
        }
    }];
}
- (void)FoundTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (self.cell  == nil) {
        self.cell = [[PlayPictureTableViewCell alloc]initWithStyle:0 reuseIdentifier:ID];
    }
    PlayPictureModel *model = self.array[indexPath.item];
    self.cell.playPictureModel = model;
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.hidesBottomBarWhenPushed = YES;
    PlayPictureModel *model = self.array[indexPath.item];
    GuideHomeViewController *guideHomeViewController = [[GuideHomeViewController alloc]init];
    guideHomeViewController.str = [NSString stringWithFormat:@"%@",model.Id];
    ToolModel *toolModel = [[ToolModel alloc]init];
    toolModel.title = model.title;
    toolModel.cover_image_url = model.cover_image_url;
    toolModel.Id = model.Id;
    guideHomeViewController.str = toolModel.Id;
    guideHomeViewController.STR = toolModel.title;
    guideHomeViewController.IMAGE = toolModel.cover_image_url;
    [self.navigationController pushViewController:guideHomeViewController animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

-(void)clickLeft:(UIBarButtonItem*)qwe{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
